﻿using System.Collections.Generic;
using System.Text;
using BruteForceSerializer.Normalization;
using System;
using BruteForceSerializer.Tests.TestClasses;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeResolve;

namespace BruteForceSerializer.Tests
{
	[TestClass]
    public class test_TypeResolver
    {
		[TestMethod]
		public void TypeToString()
		{
			Assert.AreEqual("int", TypeResolver.TypeToString(typeof(int), null));
			Assert.AreEqual("int[1]=3", TypeResolver.TypeToString(typeof(int[]), new int[] { 1, 2, 3 }));
			Assert.AreEqual("int[2]=2,3", TypeResolver.TypeToString(typeof(int[,]), new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }));
			Assert.AreEqual("int[1][1]=2", TypeResolver.TypeToString(typeof(int[][]), new int[][] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 } }));
			Assert.AreEqual("List<int>", TypeResolver.TypeToString(typeof(List<int>), null));
			Assert.AreEqual("List<List<int>>", TypeResolver.TypeToString(typeof(List<List<int>>), null));
			Assert.AreEqual("Dictionary<int,string>", TypeResolver.TypeToString(typeof(Dictionary<int, string>), null));
			Assert.AreEqual("List<int[1]>", TypeResolver.TypeToString(typeof(List<int[]>), null));
			Assert.AreEqual("List<int>[1]", TypeResolver.TypeToString(typeof(List<int>[]), null));
			Assert.AreEqual("List<int[3]>", TypeResolver.TypeToString(typeof(List<int[, ,]>), null));
			Assert.AreEqual("BruteForceSerializer.Tests.TestClasses.KlasseMitGenerics<int,string,bool>", TypeResolver.TypeToString(typeof(KlasseMitGenerics<int, string, bool>), null));
			Assert.AreEqual("BruteForceSerializer.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse1<int,string,bool>", TypeResolver.TypeToString(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse1), null));
			Assert.AreEqual("BruteForceSerializer.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse2`1<int,string,bool,long>", TypeResolver.TypeToString(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse2<long>), null));
			Assert.AreEqual("List`1", TypeResolver.TypeToString(typeof(List<>), null));
			Assert.AreEqual("Type", TypeResolver.TypeToString(typeof(Type), null));
		}

		[TestMethod]
		public void Resolve()
		{
			int[] arrayDimensions;
			Assert.AreEqual(typeof(int), TypeResolver.Resolve("int", out arrayDimensions));
			Assert.AreEqual(typeof(int[]), TypeResolver.Resolve("int[1]=3", out arrayDimensions));
			Assert.AreEqual(1, arrayDimensions.Length);
			Assert.AreEqual(3, arrayDimensions[0]);
			Assert.AreEqual(typeof(int[,]), TypeResolver.Resolve("int[2]=2,3", out arrayDimensions));
			Assert.AreEqual(2, arrayDimensions.Length);
			Assert.AreEqual(2, arrayDimensions[0]);
			Assert.AreEqual(3, arrayDimensions[1]);
			Assert.AreEqual(typeof(int[][]), TypeResolver.Resolve("int[1][1]=2", out arrayDimensions));
			Assert.AreEqual(1, arrayDimensions.Length);
			Assert.AreEqual(2, arrayDimensions[0]);
			Assert.AreEqual(typeof(List<int>), TypeResolver.Resolve("List<int>", out arrayDimensions));
			Assert.AreEqual(typeof(List<List<int>>), TypeResolver.Resolve("List<List<int>>", out arrayDimensions));
			Assert.AreEqual(typeof(Dictionary<int, string>), TypeResolver.Resolve("Dictionary<int,string>", out arrayDimensions));
			Assert.AreEqual(typeof(Dictionary<int, Dictionary<int, string>>), TypeResolver.Resolve("Dictionary<int,Dictionary<int,string>>", out arrayDimensions));
			Assert.AreEqual(typeof(List<int[]>), TypeResolver.Resolve("List<int[1]>", out arrayDimensions));
			Assert.AreEqual(typeof(List<int>[]), TypeResolver.Resolve("List<int>[1]", out arrayDimensions));
			Assert.AreEqual(typeof(List<int[, ,]>), TypeResolver.Resolve("List<int[3]>", out arrayDimensions));
			Assert.AreEqual(typeof(KlasseMitGenerics<int, string, bool>), TypeResolver.Resolve("BruteForceSerializer.Tests.TestClasses.KlasseMitGenerics<int,string,bool>", out arrayDimensions));
			Assert.AreEqual(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse1), TypeResolver.Resolve("BruteForceSerializer.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse1<int,string,bool>", out arrayDimensions));
			Assert.AreEqual(typeof(KlasseMitGenerics<int, string, bool>.InnereKlasse2<long>), TypeResolver.Resolve("BruteForceSerializer.Tests.TestClasses.KlasseMitGenerics`3+InnereKlasse2`1<int,string,bool,long>", out arrayDimensions));
			Assert.AreEqual(typeof(List<>), TypeResolver.Resolve("List`1", out arrayDimensions));
			Assert.AreEqual(typeof(Type), TypeResolver.Resolve("Type", out arrayDimensions));
		}
    }
}